﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.DAO;
using ESHOP.Domain;
using E_Shop.Common;
using ESHOP.UTIL;
namespace E_Shop
{
    public partial class CompanyInquiryCart : Authentication
    {
        private readonly TheyeucauDAO _theyeucau = new TheyeucauDAO();
        private readonly SellLeadDao _selldao = new SellLeadDao();
        private readonly BuyLeadDAO _buydao = new BuyLeadDAO();
        private readonly HopthuDAO _emaildao = new HopthuDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            else
            {
                BindDataForGrid();
                BinDataForBuying();
            }
        }
        public void BindDataForGrid()
        {
            var lsrequire = _theyeucau.GetListForCompany_Selling(Acount.MACT);
            gvListRequire.DataSource = lsrequire;
            gvListRequire.DataBind();
            UpGrid.Update();
        }
        public void BinDataForBuying()
        {
            var lsrequire = _theyeucau.GetListForCompany_Buying(Acount.MACT);
            gvListRequirebuying.DataSource = lsrequire;
            gvListRequirebuying.DataBind();
            Updatebuying.Update();
        }
        protected void gvListRequire_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListRequire.PageIndex = e.NewPageIndex;
            gvListRequire.DataBind();
        }

        protected void gvListRequire_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var id = e.CommandArgument.ToString();
            var objdb = _theyeucau.Get(id);
            var idname = e.CommandName;
            switch (idname)
            {
                case "deleterequire":
                    {
                        if (objdb != null)
                        {
                            if (_theyeucau.Delete(objdb.ID) == 1)
                            {
                                ShowInfor("Delete require cart success.");
                                BindDataForGrid();
                                UpGrid.Update();
                            }
                            else
                            {
                                ShowError("Delete require cart Fail.");
                            }
                           
                        }
                        break;
                    }
                case "Contact":
                    {
                        var products = _selldao.Get(objdb.SELLING);
                        Session[Constants.SESSION_PRODUCT] = products;
                        UnblockDialog("replyemail");
                        lbreceiver.Text = products.CONGTY.TENCONGTY.ToString();
                        lbngaygui.Text = DateTime.Now.ToString();
                        Upreplyemail.Update();
                        break;
                    }
            
            }
        }

       

      

        protected void gvListRequire_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvListRequirebuying_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListRequirebuying.PageIndex = e.NewPageIndex;
            gvListRequirebuying.DataBind();
        }

        protected void gvListRequirebuying_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var id = e.CommandArgument.ToString();
            var objdb = _theyeucau.Get(id);

            var idname = e.CommandName;
            switch (idname)
            {
                case "deleterequire":
                    {
                        if (objdb != null)
                        {
                            if (_theyeucau.Delete(objdb.ID) == 1)
                            {
                                ShowInfor("Delete require cart success.");
                                BindDataForGrid();
                                Updatebuying.Update();
                            }
                            else
                            {
                                ShowError("Delete require cart Fail.");
                            }
                           
                        }
                        break;
                    }
                case "Contact":
                    {
                        var products = _buydao.Get(objdb.BUYING);
                        Session[Constants.SESSION_PRODUCT] = products;
                        UnblockDialog("replyemail");
                        lbreceiver.Text = products.CONGTY.TENCONGTY.ToString();
                        lbngaygui.Text = DateTime.Now.ToString();
                        Upreplyemail.Update();
                        break;


                       
                    }
               
            }

        }

        protected void gvListRequirebuying_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
        protected void btnsend_Click(object sender, EventArgs e)
        {
            HideDialog("replyemail");
            var product = (SELL)Session[Constants.SESSION_PRODUCT];
            HOPTHU _emmail = new HOPTHU();
            _emmail.ID = _emaildao.NewId();
            _emmail.SUBJECT = String.IsNullOrEmpty(txtSubject.Text.Trim()) ? "" : txtSubject.Text.Trim();
            _emmail.CONTENT_MSG = txtcontent.Text.Trim() == null ? "" : txtcontent.Text.Trim();
            _emmail.SENDER = Acount.MACT;
            _emmail.RECEIVER = product.MACONGTY.ToString(); 
            _emmail.IsRead = false;
            _emmail.ISSHOW = true;
            _emmail.NGAYGUI = DateTime.Now;
            int kq = _emaildao.Insert(_emmail);
            if (kq == 1)
            {
                ShowInfor("Send email success.");
            }

            else
            {
                ShowError("Send email fail!.");
            }
            Session[Constants.SESSION_PRODUCT] = null;
        }
    }
}